package Util::TuanZhuanYi::TuanZhuanYi;
use Moose;
use namespace::autoclean;
use JSON;
use Time::Piece;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#	user_id	
#Return: 
#	requests		
#####################################################
sub find {
	my ($self, $tzy_id) = @_;

	return  $self->schema->resultset('TuanZhuanYi')->search({id => $tzy_id}, {}
	)->first();
}


sub zhuanyi_request_search {
	my ($self, $user_id) = @_;

	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [$user_id, $user_id]	
	}
	);
	#my @requests1 =  $self->schema->resultset('TuanZhuanYi')->search({shangyou_user_id => $user_id }, {});
	#my @requests2 =  $self->schema->resultset('TuanZhuanYi')->search({xiayou_user_id => $user_id }, {});
	#my @requests1 = $self->schema->resultset('TuanZhuanYiComplex')->search(
	#	{'shangyou_user_id' => $user_id},
	#	{
	#		select => [
	#			'users.username',
	#			'tuan.image',
	#			'shangyou_user_id',
	#			'xiayou_user_id',
	#			'tuan.name1',
	#			'tuan1.name',
	#			'status',
	#			'created'
	#		],
	#		as => [qw/
	#			xiayou_tuanzhang_name
	#			tuan_image
	#			shangyou_user_id
	#			xiayou_user_id
	#			shangyou_tuan_name
	#			xiayou_tuan_name
	#			status
	#			created
	#		/],
	#		order_by=>{-desc=>"created"},
	#		join => ['users','tuan','tuan']
	#	}
	#);
				
	#my @array1 = (@requests1,@requests2);
	#my @array1 = (@requests1);
				
	#return @array1; 

}

sub request_tuan_zhuanyi {
	my ($self, $shangyou_user_id, $xiayou_user_id, $shangyou_tuan_id, $zhuanyi_univ_id, $xiayou_percentage, $req_detail, $status, $req_user_id) = @_;

	my $request = $self->schema->resultset('TuanZhuanYi')->create({
			shangyou_user_id => $shangyou_user_id,
			xiayou_user_id => $xiayou_user_id,
			shangyou_tuan_id => $shangyou_tuan_id,
			zhuanyi_univ_id => $zhuanyi_univ_id,
			xiayou_percentage => $xiayou_percentage,
			req_detail => $req_detail,
			status => $status,
			created_by => $req_user_id,
			updated_by => $req_user_id
			});
	return $request;
}


__PACKAGE__->meta->make_immutable;

1;
